package com.itsm.controller;

import com.alibaba.fastjson.JSONObject;
import com.itsm.entity.*;
import com.itsm.entity.Process;
import com.itsm.service.ApproveService;
import com.itsm.service.LoginService;
import com.itsm.service.ProcessUserService;
import com.itsm.service.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 */
@RestController
public class ProcessUserController {

    @Autowired
    private ProcessUserService processUserService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private ApproveService approveService;

    @Autowired
    private UserMapper userMapper;
    /**
     * 创建工单
     * @param  processCreate 工单信息
     * @return 行数
     */
    @PostMapping("/add_process")
    public Integer add(@RequestBody ProcessCreate processCreate, HttpServletRequest request) throws IOException {
        //获取人员登录基本信息
        String name = (String) request.getSession().getAttribute("name");
        String staffNum = (String) request.getSession().getAttribute("alias");
        String userid = (String) request.getSession().getAttribute("userid");
        //人员一级部门
        String org = loginService.selectOrg1(name,staffNum);
        Process process = new Process();
        List<String> listName = new ArrayList<>();
        process.setCreateName(name);
//        List<FileName> fileNames = (List<FileName>) processCreate.getFileList();
        //强制转换类型，将object转化成List，获取文件列表信息
        if(processCreate.getFileList()!=null){
            List<FileName> fileNames = JSONObject.parseArray(JSONObject.toJSONString(processCreate.getFileList()), FileName.class);
            for (FileName fileName : fileNames) {
                listName.add(fileName.getName());
            }
            process.setFileList(StringUtils.strip(listName.toString(),"[]"));
        }
        process.setOrgName(org);
        process.setUserid(userid);
        process.setStaffNum(staffNum);
        process.setContent(processCreate.getContent());
        process.setProposer(processCreate.getProposer());
        process.setQuestionPhoto(processCreate.getQuestionPhoto());
        process.setSubject(processCreate.getSubject());
        process.setQuestionType(processCreate.getQuestionType());
        return processUserService.addProcess(process,processCreate.getApprove1(),processCreate.getApprove2());
    }




    @GetMapping("getPro")
    public Process getProcessOne(String processNo){
//        System.out.println(processUserService.getUserProcess(processNo));
        return processUserService.getUserProcess(processNo);
    }

    /**
     *查询全部工单（创建人的）
     * @param request
     * @return
     */
    @GetMapping("userProAll")
    public List<Process> getUserProcessAll(HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        return processUserService.selectUserProcess(name,null);
    }

    /**
     * 待接收工单查询（刚创建）
     * @param request 通过request获取名字并查询数据
     * @return
     */
    @GetMapping("/userPro")
    public List<Process> query(HttpServletRequest request) {
        String name = (String) request.getSession().getAttribute("name");
        return processUserService.selectUserProcess(name,0);
    }

    /**
     * 已指派的工单（未完成工单）
     * @param request 获取名字
     * @return
     */
    @GetMapping("userPro1")
    public List<Process> getUserProcess1(HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        return processUserService.selectUserProcess(name,1);
    }

    /**
     * 待评价的工单（已完成需要评价）
     * @param request
     * @return
     */
    @GetMapping("userPro2")
    public List<Process> getUserProcess2(HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        return processUserService.selectUserProcess(name,2);
    }

    /**
     * 完成未评价的工单
     * @param request
     * @return
     */
    @GetMapping("userPro3")
    public List<Process> getUserProcess3(HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        return processUserService.selectUserOver(name,2);
    }
    /**
     * 已完成已评价的工单
     * @param request
     * @return
     */
    @GetMapping("userPro4")
    public List<Process> getUserProcess4(HttpServletRequest request){
        String name = (String) request.getSession().getAttribute("name");
        return processUserService.selectUserOver1(name,2);
    }



}
